﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Media;

namespace Platformer
{
    class PurpleFireball
    {
        public Texture2D sprite;
        public Vector2 position;
        public float rotation;
        public Vector2 velocity;
        public bool alive, first = true, once = true;
        public Vector2 startPosition;
        private int face;

        private AnimationPlayer player;
        //private AnimationPlayer player;
        Level level;
        Enemy enemy;
        private Animation purpleFireballCollision, purpleSkull;

        //private Animation purpleFireballCollision;

        public bool collided = false;

        public bool checkAlive
        {
            get { return alive; }
        }

        public void LoadContent()
        {
            purpleFireballCollision = new Animation(level.Content.Load<Texture2D>("Sprites/Boss/bossCollision"), 0.1f, false);
            purpleSkull = new Animation(level.Content.Load<Texture2D>("Sprites/Boss/bossSkull"), 0.5f, false);

        }

        public Rectangle rectangle
        {
            get
            {
                int left = (int)position.X;
                int width = sprite.Width;
                int top = (int)position.Y;
                int height = sprite.Height;
                return new Rectangle(left, top, width, height);
            }
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            SpriteEffects flip = new SpriteEffects();
            if (alive)
            {
                spriteBatch.Draw(sprite, position, Color.White);
                if (face == -1) 
                {
                    flip = face < 0 ? SpriteEffects.FlipHorizontally : SpriteEffects.None;
                    player.Draw(gameTime, spriteBatch, new Vector2(position.X + 50, position.Y + 40), flip);
                }
                else
                player.Draw(gameTime, spriteBatch, new Vector2(position.X + 50, position.Y + 40), SpriteEffects.None);
            }
            if (collided)
                player.Draw(gameTime, spriteBatch, new Vector2(position.X, position.Y + 40), SpriteEffects.None);
        }

        public PurpleFireball(Level l, Vector2 pos, int dir)
        {
            this.level = l;
            rotation = 0.0f;
            sprite = level.Content.Load<Texture2D>("Sprites/Player/IceSpear");
            position = new Vector2(pos.X, pos.Y - 40);
            startPosition = position;
            if (dir == -1)
            {
                velocity = new Vector2(-10.5f, 0.0f);
                face = -1;
            }
            else
            {
                velocity = new Vector2(10.5f, 0.0f);
                face = 1;
            }

            alive = true;
            LoadContent();
        }

        public void UpdatePurpleFireball()
        {
            //Only update them if they're alive
            if (alive)
            {

                player.PlayAnimation(purpleSkull);
                //Move our purpleFireball based on it's velocity
                position += velocity;

                //Rectangle the size of the screen so purpleFireballs that
                //fly off screen are deleted.

                Rectangle screenRect = new Rectangle(0, 0, 800, 600);
                if (!screenRect.Contains(new Point(
                    (int)position.X,
                    (int)position.Y)))
                {
                    alive = false;
                    collided = true;
                }

                //Collision rectangle for each purpleFireball -Will also be
                //used for collisions with enemies.
                Rectangle purpleFireballRect = new Rectangle(
                    (int)position.X,
                    (int)position.Y,
                    sprite.Width,
                    sprite.Height);

                //Check for collisions with the player
                if (purpleFireballRect.Intersects(level.Player.BoundingRectangle))
                {
                    level.Player.currentHP -= 100;
                    if (level.Player.currentHP <= 0)
                        level.Player.OnKilled(enemy);


                    alive = false;
                    collided = true;
                }



                //Look for adjacent tiles to the purpleFireball
                Rectangle bounds = new Rectangle(
                    purpleFireballRect.Center.X,
                    purpleFireballRect.Center.Y,
                    purpleFireballRect.Width / 4,
                    purpleFireballRect.Height / 4);
                int leftTile = (int)Math.Floor((float)bounds.Left / Tile.Width);
                int rightTile = (int)Math.Ceiling(((float)bounds.Right / Tile.Width)) - 1;
                int topTile = (int)Math.Floor((float)bounds.Top / Tile.Height);
                int bottomTile = (int)Math.Ceiling(((float)bounds.Bottom / Tile.Height)) - 1;

                // For each potentially colliding tile
                for (int y = topTile; y <= bottomTile; ++y)
                {
                    for (int x = leftTile; x <= rightTile; ++x)
                    {
                        TileCollision collision = level.GetCollision(x, y);

                        //If we collide with an Impassable or Platform tile
                        //then delete our purpleFireball.
                        if (collision == TileCollision.Impassable ||
                            collision == TileCollision.Platform)
                        {
                            if (purpleFireballRect.Intersects(bounds))
                            {
                                alive = false;
                                collided = true;
                            }
                        }
                    }
                }
            }
            if (collided)
            {
                player.PlayAnimation(purpleFireballCollision);
            }

        }
    }
}
